package main

import (
	"fmt"
	"unsafe"

	"golang.org/x/sys/windows"
	"golang.org/x/sys/windows/clipboard"
)

func getClipboardText() (string, error) {
	err := clipboard.Open(0)
	if err != nil {
		return "", err
	}
	defer clipboard.Close()

	available, err := clipboard.IsFormatAvailable(clipboard.CF_TEXT)
	if err != nil || !available {
		return "", err
	}

	h, err := clipboard.GetData(clipboard.CF_TEXT)
	if err != nil {
		return "", err
	}

	buf := make([]byte, 256)
	length, err := clipboard.GetText(buf, 256)
	if err != nil {
		return "", err
	}

	return windows.UTF16ToString((*[256]uint16)(unsafe.Pointer(&buf))[:length]), nil
}

func main() {
	text, err := getClipboardText()
	if err != nil {
		fmt.Println("Failed to get clipboard text:", err)
		return
	}
	fmt.Println("Clipboard text:", text)
}
